package org.javaboy.demo;

import okhttp3.OkHttpClient;
import org.springframework.beans.factory.FactoryBean;

public class OkHttpClientFactoryBean implements FactoryBean<OkHttpClient> {
    public OkHttpClientFactoryBean() {
        System.out.println("OkHttpClientFactoryBean");
    }

    /**
     * 这个方法要等到去 Spring 容器获取 OkHttpClient 的时候，这个方法才会执行
     * @return
     * @throws Exception
     */
    @Override
    public OkHttpClient getObject() throws Exception {
        System.out.println("getObject");
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.setConnectTimeout$okhttp(5000);
        builder.setReadTimeout$okhttp(5000);
        return builder
                .build();
    }

    @Override
    public Class<?> getObjectType() {
        return OkHttpClient.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
